package com.work.game.refresh.init;

import android.app.Application;
import android.content.Context;
import android.graphics.Bitmap;

import androidx.multidex.MultiDex;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.bytedance.applog.AppLog;
import com.bytedance.applog.InitConfig;
import com.bytedance.applog.util.UriConstants;
import com.github.gzuliyujiang.oaid.DeviceIdentifier;
import com.github.gzuliyujiang.oaid.IRegisterCallback;
import com.umeng.commonsdk.UMConfigure;
import com.work.game.config.UrlConfig;
import com.work.game.home.ui.model.vo.AppLogVo;
import com.work.game.utils.risk.DeviceUtils;
import com.work.game.volley.httpUtil.HCUParame;
import com.work.game.volley.httpUtil.HUCRequest;
import com.work.game.volley.httpUtil.interfase.HUCCallBackInterfase;
import com.work.game.volley.httpUtil.interfase.HUCParameInterfase;
import com.work.game.volley.httpUtil.interfase.HUCRequestInterfase;
import com.work.game.volley.state.HUCState;

import java.io.PrintWriter;
import java.io.StringWriter;


/**
 * For developer startup JPush SDK
 * <p>
 * 一般建议在自定义 Application 类里初始化。也可以在主 Activity 里。
 */
public class GameApplication extends Application implements Thread.UncaughtExceptionHandler, HUCCallBackInterfase {

    private static GameApplication instance;
    private RequestQueue mQueue;
    // 网络请求接口类
    public HUCRequestInterfase hucRequestInterfase;
    // 网络请求构参接口类
    public HUCParameInterfase hucParameInterfase;

    private DeviceUtils deviceUtils;
    private Thread.UncaughtExceptionHandler defaultUEH;


    private static final String TAG = "JIGUANG-Example";

    @Override
    public void onCreate() {
        // Logger.d(TAG, "[ExampleApplication] onCreate");
        super.onCreate();
        instance = this;
        mQueue = Volley.newRequestQueue(this); //初始化 volley
        deviceUtils = DeviceUtils.getInstance();
        hucRequestInterfase = HUCRequest.getInstance();
        hucParameInterfase = HCUParame.getInstance();
        Thread.setDefaultUncaughtExceptionHandler(this);
        this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
    }

    public static GameApplication getMyApplication() {
        return instance;
    }

    public RequestQueue getRequestQueue() {
        return mQueue;
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
    }


    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private void initAppLog() {
        /* 初始化开始，appid和渠道，appid如不清楚请联系客户成功经理
         * 注意第二个参数 channel 不能为空
         */
        final InitConfig config = new InitConfig("654316", "rangersapplog.9e49a268c6d6bcec");
        //上报地址
        config.setUriConfig(UriConstants.DEFAULT);
        // 加密开关，SDK 5.5.1 及以上版本支持，false 为关闭加密，上线前建议设置为 true
        AppLog.setEncryptAndCompress(true);

        /* 初始化结束 */
        config.setAutoStart(true);

        AppLog.init(this, config);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable ex) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        ex.printStackTrace(pw);
        String exception = sw.toString();

        // 上传闪退日志到服务器
        // 尝试发送异常信息到服务器
        try {
            uploadCrashLog(exception);
        } catch (Exception sendingException) {
            // 发送异常失败时的处理
            // ...
        }
        defaultUEH.uncaughtException(thread, ex);
        // 退出应用程序
        System.exit(1);
    }

    private void uploadCrashLog(String exception) {
        // 实现上传闪退日志的逻辑
        // 可以使用网络请求库将日志上传到服务器
        AppLogVo appLogVo = new AppLogVo();
        appLogVo.setAlAndroid(deviceUtils.getSystemAndroidVersionCode());
        appLogVo.setAlBrand(deviceUtils.getSystemAndroidMobilePhoneBrand());
        appLogVo.setAlSystemVersionCode(deviceUtils.getSystemAndroidSystemVersionCode());
        appLogVo.setAlContent(exception);
        hucRequestInterfase.executeStr(this, null, HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.URL_APP_LOG, hucParameInterfase.getRequestParameter(appLogVo), true);
    }


    @Override
    public void onSuccessfulStrType(int inputType, Object response) {

    }

    @Override
    public void onFailStrType(int inputType, Object response) {

    }

    @Override
    public void onSuccessfulBitampType(int inputType, Bitmap response) {

    }

    @Override
    public void onErrorResponse(int inputType, VolleyError response) {

    }

    @Override
    public void onErrorLogin(int inputType, Object response) {

    }

    @Override
    public void onResetLogin(Object response) {

    }
}
